package com.hp.hunter

import com.hp.define.AppInfo
import com.hp.define.BaseState
import com.hp.define.MultiState
import com.hp.define.State
import org.apache.commons.io.FileUtils

public class FileManager {
    private String dir = null
    private String rootPath = null
    private String[] allowFiles = null
    private int count = 0

    public FileManager(Map<String, Object> conf) {
        this.rootPath = (String)conf.get("rootPath")
        this.dir = this.rootPath + (String)conf.get("dir")
        this.allowFiles = this.getAllowFiles(conf.get("allowFiles"))
        this.count = ((Integer)conf.get("count")).intValue()
    }

    public State listFile(int index) {
        File dir = new File(this.dir)
        State state = null
        if (!dir.exists()) {
            return new BaseState(false, AppInfo.ERROR_302_NOT_EXIST)
        } else if (!dir.isDirectory()) {
            return new BaseState(false, AppInfo.ERROR_301_NOT_DIRECTORY)
        } else {
            Collection<File> list = FileUtils.listFiles(dir, this.allowFiles, true)
            if (index >= 0 && index <= list.size()) {
                Object[] fileList = Arrays.copyOfRange(list.toArray(), index, index + this.count)
                state = this.getState(fileList)
            } else {
                state = new MultiState(true)
            }

            ((State)state).putInfo("start", (long)index)
            ((State)state).putInfo("total", (long)list.size())
            return (State)state
        }
    }

    private State getState(Object[] files) {
        MultiState state = new MultiState(true)
        BaseState fileState = null
        File file = null
        Object[] var8 = files
        int var7 = files.length

        for(int var6 = 0; var6 < var7; ++var6) {
            Object obj = var8[var6]
            if (obj == null) {
                break
            }

            file = (File)obj
            fileState = new BaseState(true)
            fileState.putInfo("url", PathFormat.format(this.getPath(file)))
            state.addState(fileState)
        }

        return state
    }

    private String getPath(File file) {
        String path = file.getAbsolutePath()
        return path.replace(this.rootPath, "/")
    }

    private String[] getAllowFiles(Object fileExt) {
        String[] exts = null
        String ext = null
        if (fileExt == null) {
            return new String[0]
        } else {
            exts = (String[])fileExt
            int i = 0

            for(int len = exts.length; i < len; ++i) {
                ext = exts[i]
                exts[i] = ext.replace(".", "")
            }

            return exts
        }
    }
}
